package com.project.service.impl;

import com.project.bean.ShoppingCarBean;
import com.project.mapper.IShoppingCarMapper;
import com.project.service.IShoppingCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author(王思翰)
 * @Description(类的描述)
 * @Date(创建时间) 2021/8/1 12:15
 */
@Service
@Transactional
public class ShoppingCarServiceImpl implements IShoppingCarService {
    @Autowired
    private IShoppingCarMapper mapper;

    @Override
    public List<ShoppingCarBean> findByUser(int userId) {
        return mapper.findByUser(userId);
    }

    @Override
    public void add(int userId, int productId) {
        ShoppingCarBean shoppingCarBean=mapper.selectProduct(userId,productId);
        if (shoppingCarBean!=null){
            this.update(shoppingCarBean.getId(),shoppingCarBean.getNum()+1);
        }else {
            mapper.add(userId,productId);
        }
    }

    @Override
    public void update(int id, int number) {
        mapper.update(id,number);
    }

    @Override
    public void del(int id) {
        mapper.del(id);
    }
}
